package ua.kiev.tosik.price.modules.clients.gui.prices;

import org.apache.log4j.Logger;
import org.pushingpixels.substance.api.SubstanceLookAndFeel;
import org.pushingpixels.substance.api.tabbed.TabCloseListener;
import ua.kiev.tosik.price.modules.clients.gui.files.IPricesTab;
import ua.kiev.tosik.price.modules.clients.gui.files.PricesFilesPanel;

import javax.swing.*;
import java.awt.*;
import java.util.HashMap;

public class PricesCenterPanel {

	private JTabbedPane centerPanel;
	private static Logger logger = Logger.getLogger("ua.kiev.tosik.price.modules.clients.gui.files");
	// private PricesFilesPanel panel;
	private PricesFilesPanel filesPanel;

	private HashMap<String, IPricesTab> tabList;

	/*
	private LockableUI lockableUI;
	private JXLayer<JComponent> layer;
	*/

	public PricesCenterPanel() {
		centerPanel = new JTabbedPane(JTabbedPane.TOP);
		// panel = new PricesFilesPanel();
		tabList = new HashMap<String, IPricesTab>();

		filesPanel = new PricesFilesPanel();
		addTab(filesPanel.getTitle(), filesPanel);

		/*
		lockableUI = new LockableUI();
		layer = new JXLayer<JComponent>(centerPanel, lockableUI);
		lockPanel(false);
		*/

		centerPanel.putClientProperty(SubstanceLookAndFeel.TABBED_PANE_CLOSE_BUTTONS_PROPERTY, Boolean.TRUE);
		((JComponent)centerPanel.getComponentAt(0)).putClientProperty(SubstanceLookAndFeel.TABBED_PANE_CLOSE_BUTTONS_PROPERTY, Boolean.FALSE);

		SubstanceLookAndFeel.registerTabCloseChangeListener(centerPanel, new TabCloseListener() {
			private String tabName;
			public void tabClosing(JTabbedPane tabbedPane, Component tabComponent) {
				logger.debug("Tab [" + tabbedPane.getTitleAt(tabbedPane.indexOfComponent(tabComponent)) + "] closing. ");
				tabName = tabbedPane.getToolTipTextAt(tabbedPane.indexOfComponent(tabComponent));
			}
			public void tabClosed(JTabbedPane tabbedPane, Component tabComponent) {
				logger.debug("Tab closed");
				IPricesTab tab = tabList.get(tabName);
				tab.destroy();
				tabList.remove(tabName);
				tabName = null;
			}
		});

	}

	public JComponent getCenterPanel() {
		return centerPanel;
	}

	/*
	public synchronized void lockPanel(boolean lock) {
		lockableUI.setLocked(lock);
	} */

	public void addTab(String uniqueId, IPricesTab component) {
		if ( !isTabExists(uniqueId) ) {
			centerPanel.addTab(component.getTitle(), component.getImageIcon(), component.getPanel(), uniqueId);
			tabList.put(uniqueId, component);
			centerPanel.setSelectedIndex(centerPanel.getTabCount()-1);
			logger.debug("Tab [" + component.getTitle() + "] added to workspace");
		} else {
			logger.debug("Tab [" + component.getTitle() + "] already in workspace");
		}
	}

	public PricesFilesPanel getFilesPanel() {
		return filesPanel;
	}

	public boolean isTabExists(String tabId) {
		return tabList.containsKey(tabId);
	}

	public void removeAllTabs() {
		while ( centerPanel.getTabCount() > 1 ) {
			centerPanel.removeTabAt(centerPanel.getTabCount()-1);
		}
		tabList.clear();
	}

	public synchronized void removeTab(IPricesTab component) {
		for ( String tabId: tabList.keySet() ) {
			if ( component == tabList.get(tabId) ) {
				for ( int i = 0; i < centerPanel.getTabCount(); i++ ) {
					if ( centerPanel.getToolTipTextAt(i).equals(tabId) ) {
						logger.debug("Tab [" + centerPanel.getToolTipTextAt(i) + "] closed");
						centerPanel.removeTabAt(i);
						tabList.remove(tabId);
						break;
					}
				}
				break;
			}
		}
	}

	public void removeTabById(String uniqueId) {
		for ( int i = 0; i < centerPanel.getTabCount(); i++ ) {
			if ( centerPanel.getToolTipTextAt(i).equals(uniqueId) ) {
				centerPanel.removeTabAt(i);
				tabList.remove(uniqueId);
				logger.debug("Tab [" + uniqueId + "] removed");
			}
		}
	}
}
